package com.googydiary.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.googydiary.bean.UserBean;

public class LoginFactory {
	Connection connection = null;
	PreparedStatement ptmt = null;
	ResultSet resultSet = null;

	public LoginFactory() {

	}

	private Connection getConnection() throws SQLException {
		Connection conn;
		conn = ConnectionFactory.getInstance().getConnection();
		return conn;
	}

	public boolean ValidateLogin(UserBean bean) {
		try {
			String queryString = "SELECT * FROM logininfo WHERE UserName='"
					+ bean.getUserName().toString() + "'";
			connection = getConnection();
			ptmt = connection.prepareStatement(queryString);
			resultSet = ptmt.executeQuery();
			resultSet.next();
			if (resultSet.wasNull()) {
				return false;
			} else if (resultSet.getString("password").equals(
					bean.getPassword().toString())) {

				return true;

			} else {

				return false;

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			try {
				if (resultSet != null)
					resultSet.close();
				if (ptmt != null)
					ptmt.close();
				if (connection != null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}
